/**
 * 用户档案里面项目列表
 */

function project_info() {
    var _this = this;
    var page;
    _this.page_size = 10;
    _this.type;
    _this.projectName;

    _this.init = function (page_person, type, projectName) {
        page = page_person;
        _this.initTable();
        _this.type = type;
        _this.projectName = projectName;

        $('#project_role_list').on("click", "span", function () {
            $('#project_role_list').find("span").removeClass("active");
            $(this).addClass("active");
            _this.bind_click(this, "roleId");
        });

        $('#user_project_status').on("click", "span", function () {
            $('#user_project_status').find("span").removeClass("active");
            $(this).addClass("active");
            _this.bind_click(this, "proUserStatus");
        });
    };

    _this.initTable = function () {
        var list_url = appPath + "/admin/dossier/project/user_list";
        var project_id = $("#project_info_form").find("input[name=project_id]").val();
        var project_course_url = appPath + "/admin/dossier/person/course?project_id=" + project_id + "&user_id=";
        page.init("project_info_form", list_url, "project_info_table", "project_info_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function (dataList) {
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for (var i = 0; i < len; i++) {
                item = dataList[i];
                var exam_status = item['exam_status'];
                inner += '<tr>';
                inner += "<td><input type='checkbox' id='person_" + i + "' name='checkPerson' value='" + item['user_id'] + "' exam_status='" + exam_status + "'/></td>";
                inner += '<td>' + (i + 1) + '</td>';
                inner += '<td>' + item['user_name'] + '</td>';
                inner += '<td>';
                if("默认角色" == item['role_name']){
                    inner += '<span class="text-orange">/</span>';
                }else{
                    inner += '<span class="text-orange">'+ item['role_name'] + '</span>';
                }
                inner += '</td>';
                var company_name = item.company_name;
                if (company_name == '' || company_name == undefined || company_name == null) {
                    inner += "<td>/</td>";
                } else {
                    inner += '<td>' + item['company_name'] + '</td>';
                }

                var dept_name = item.dept_name;
                if (dept_name == '' || dept_name == undefined || dept_name == null) {
                    inner += "<td>/</td>";
                } else {
                    inner += '<td>' + item['dept_name'] + '</td>';
                }

                inner += '<td>' + TimeUtil.getHouAndMinAndSec(item['requirement_studytime'] * 60) + '</td>';
                inner += '<td>' + TimeUtil.getHouAndMinAndSec(item['total_studytime']) + '</td>';
                inner += '<td>' + item['total_question'] + '</td>';
                inner += '<td>' + item['yet_answered'] + '</td>';
                inner += '<td>' + item['correct_rate'] + '%</td>';
                /*inner += '<td>' + Number((Number(page.pageNum) - 1) * Number(page.pageSize) + Number((i + 1))) + '</td>';*/
               /* var exam_score = item['exam_score'];
                if (exam_score == '' || exam_score == undefined) {
                    exam_score = '/';
                }
                inner += '<td>' + exam_score + '</td>';

                var subjective_score = item['subjective_score'];
                if(subjective_score == '' || subjective_score == undefined){
                    subjective_score = '\\';
                }
                inner += '<td>'+ subjective_score + '</td>';*/
                inner += '<td>'+ item['total_score'] + '</td>';

                inner +='<td>'+ TimeUtil.getHouAndMinAndSec(item['exam_Time'] * 60) +'</td>';   //考试耗时
                if (exam_status == 1) {//考试状态:1未考试2合格3不合格
                    inner += '<td>未考试</td>';
                } else if (exam_status == 2) {
                    inner += '<td>合格</td>';
                } else {
                    inner += '<td>不合格</td>';
                }
                inner += '<td>';
                inner += '<a href="' + project_course_url + item['user_id'] + '" class="a a-info">详情</a> ';
                var exam_no = item['exam_no'];
                if (exam_no != undefined && exam_no != '') {
                    inner += '<a href="javascript:void(0);" onclick="common.goto_student_exam_answer_view(\'' + item['exam_no'] + '\');" class="a a-view">答题记录</a>';
                } else {
                    inner += '<a href="javascript:void(0);" onclick="layer.msg(\'没有考试记录\');" class="a">答题记录</a>';
                }
                inner += '</td>';
                inner += '</tr>';
            }

            var iObj = $(inner);
            iObj.each(function () {
                /*培训练习*/
                if (_this.type == 1 || _this.type == 2 || _this.type == 4) {
                    $(this).find("td").eq(12).html("\\");
                    $(this).find("td").eq(13).html("\\");
                }
                /*培训考试*/
                if (_this.type == 1 || _this.type == 3 ) {
                    $(this).find("td").eq(8).html("\\");
                    $(this).find("td").eq(9).html("\\");
                    $(this).find("td").eq(10).html("\\");
                }
                /*练习考试*/
                if (_this.type == 2 || _this.type == 3 || _this.type == 6) {
                    // 部门列
                    // $(this).find("td").eq(5).html("\\");
                }
                /*考试*/
                if (_this.type == 3) {
                    $(this).find("td").eq(6).html("\\");
                    $(this).find("td").eq(12).html("\\");
                }
            });

            return iObj;
        };
    };

    _this.search = function () {
        _this.initTable();
    };

    _this.searchAll = function () {
        $("#examStatus").val("");
        $("#project_info_form").find("input[name=user_name]").val("");
        $("#project_info_form").find("input[name=dept_name]").val("");
        $("#project_info_form").find("input[name=role_name]").val("");
        _this.initTable();
    };

    _this.bind_click = function (t, id) {
        var value = $(t).attr("data-value");
        $("#"+id).val(value);
        _this.initTable();
    };

    _this.export_info = function () {
        if (page.count != 0) {
            _this.to_export();
        } else {
            layer.msg("没有可以导出的数据！");
        }
    };
    /**
     * checkbox 点击事件
     * @param selectedNode
     */
    var userIdAll = [];
    var object = {};
    var exam_status;
    /**
     * 全选事件
     */
    $("#checkAll").click(function () {
            if (this.checked) {
                $("#project_info_table :checkbox").prop("checked", true);

                $("#project_info_table :checkbox").each(function () {
                    if (!object[$(this).val()]) {
                        object[$(this).val()] = 1;
                        userIdAll.push($(this).val());
                        exam_status = $(this).attr("exam_status") == "1"?false:true;
                    }
                });

            } else {
                $("#project_info_table :checkbox").prop("checked", false);
                if (userIdAll.length > 0) {
                    for (var a = 0; a < userIdAll.length; a++) {
                        $("#project_info_table :checkbox").each(function () {
                            if ($(this).val() === userIdAll[a]) {
                                delete object[$(this).val()];
                                userIdAll.splice(a, 1);
                            }
                        })
                    }
                }
            }
        }
    );

    /**
     * 单选事件
     */
    $("#project_info_table").on("click", "input[type=checkbox]", function () {
        if ($(this).is(":checked")) {
            if (!object[$(this).val()]) {
                object[$(this).val()] = 1;
                userIdAll.push($(this).val());
                exam_status = $(this).attr("exam_status") == "1"?false:true;
            }
        } else {
            if (userIdAll.length > 0) {
                for (var a = 0; a < userIdAll.length; a++) {
                    if ($(this).val() === userIdAll[a]) {
                        delete object[$(this).val()];
                        userIdAll.splice(a, 1);
                    }
                }
            }
        }

    });
    /**
     *
     * 清空所有的选择
     */
    $("#clearSelAll").on("click", function () {
        object = {};
        userIdAll = [];
    });
    //console.log(userIdAll);


    _this.to_export = function () {
        //var checkRes = _this.getCheckedRows_ids();

        var project_id = $("#project_info_form").find("input[name=project_id]").val();
        console.log("数据长度："+userIdAll);
        if (userIdAll.length>0) {
            if (exam_status == false) {       //所选 都没有考试记录
                layer.msg("没有考试记录可导出！");
                return;
            }
            var backend_url = $("#backendUrl").val();
            window.open(backend_url + '/export/export_exam_record?userIds=' + userIdAll + "&project_id=" + project_id + "&project_name=" + _this.projectName);
        } else {
            layer.msg("请选择需导出成绩的人员！");
        }
    };
    _this.getCheckedRows_ids = function () {
        // var length = $("#person_table").find("input[type=checkbox]:checked").length;
        // alert(length);
        var userIds = "";
        var exam_status = false; //  没有考试记录
        $("#project_info_table").find("input[type=checkbox]:checked").each(function () {
            userIds += userIds == '' ? $(this).val() : "," + $(this).val();
            if ($(this).attr("exam_status") != "1") {  //考试状态
                exam_status = true;
            }
        });
        return {"userIds": userIds, "exam_status": exam_status};
    };


    //人员全选
    // $("#checkAll").click(function () {
    //     var flag = $("#checkAll").prop("checked");
    //     $('#project_info_table input[type="checkbox"]').prop("checked", flag);
    // })

    _this.ajax = function (url, param, type) {
        var result;
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function (data) {
                result = data;
            }
        });
        return result;
    };


    /**
     * 培训记录导出
     */
    _this.export_train_info = function () {
        if(userIdAll.length > 0){
            _this.train_export();
        }else{
            layer.msg("请选择需导出培训记录的人员！");
            return;
        }
    };

    _this.train_export = function () {
        var project_id = $("#project_info_form").find("input[name=project_id]").val();
        if (userIdAll.length>0) {
            if(userIdAll.length>50){
                layer.msg("一次性最多导出50份培训记录，将等待两分钟！",{time:1000});
                return;
            }
            window.open(appPath + '/admin/dossier/project/trainExport?userIds=' + userIdAll.join(",") + "&projectId=" + project_id + "&projectName=" + _this.projectName + "&projectType=" + _this.type);
        } else {
            layer.msg("请选择需导出培训记录的人员！");
            return;
        }
    };
}

var project_info = new project_info();